package com.luozongyao.eduService.controller;


import com.luozongyao.commonutils.R;
import com.luozongyao.eduService.client.VodClient;
import com.luozongyao.eduService.entity.EduChapter;
import com.luozongyao.eduService.entity.EduVideo;
import com.luozongyao.eduService.service.EduVideoService;
import com.luozongyao.servicebase.config.exceptionHandler.lzyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-11
 */
@RestController
@RequestMapping("/eduService/edu-video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideoInfo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    //根据id查询章节
    @GetMapping("getVideoInfoById/{videoId}")
    public R getChapterInfoById(@PathVariable String videoId){
        EduVideo video = eduVideoService.getById(videoId);
        return R.ok().data("video",video);
    }

    //修改小节
    @PostMapping("updateVideoInfo")
    public R updateVideoInfo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }

    //删除小节
    //TODO后面这个方法需要完善:删除小节时候，同时把阿里云里面视频删除
    @DeleteMapping("removeVideo/{courseId}")
    public R removeVideoById(@PathVariable("courseId") String courseId){

        //根据小节id获取视频id，调用方法实现视频删除
        EduVideo video = eduVideoService.getById(courseId);
        String VideoSourceId = video.getVideoSourceId();

        //判断小节是否有视频ID
        if (!StringUtils.isEmpty(VideoSourceId)){
            //根据视频id，远程调用实现视频删陈
            R result = vodClient.removeVideo(VideoSourceId);
            if (result.getCode()==20001){
                throw new lzyException(20001,"删除视频失败,熔断器");
            }
        }
        //删除小节
        eduVideoService.removeById(courseId);
        return R.ok();
    }




}

